################################################################# 
#Replication: Walter et al. Non-Cooperation by popular vote     #
#             International Organization                        #
#################################################################



#Please read readme.txt before using this file.


library(MASS)
library(Matrix)
library(mediation)
library(foreign)
library(survey)
library(gridExtra)
library(ggplot2)
setwd("/path/to/replication/directory/")


##########
#Figure 3#
##########

graphing <- read.dta("withvars.dta")
df2<-data.frame(graphing)

voteref<-ggplot(df2, aes(factor(refall)))+ geom_bar(aes(y = (..count..)/sum(..count..)*100),width=.5) +
  geom_text(aes(y = ((..count..)/sum(..count..)*100), label = scales::percent((..count..)/sum(..count..))),size=3, stat = "count", vjust = -0.25) +
  ylab("Percentage") + xlab("") +
  theme(axis.title.x = element_text(size = 8), axis.title.y = element_text(size = 8), 
        axis.text.x = element_text(size = 8),axis.text.y = element_text(size = 8), 
        plot.title = element_text(face="bold", size=10,hjust = 0.5,vjust = -1))  +
  scale_y_continuous(limits = c(0,60)) +
  ggtitle("Vote Intentions") 
voteref

exp<-ggplot(df2, aes(factor(ResExp2)))+ geom_bar(aes(y = (..count..)/sum(..count..)*100),width=.7) +
  geom_text(aes(y = ((..count..)/sum(..count..)*100), label = scales::percent((..count..)/sum(..count..))),size=3, stat = "count", vjust = -0.25) +
  ylab("Percentage") + xlab("") + 
  theme(axis.title.x = element_text(size = 8), axis.title.y = element_text(size = 8), 
        axis.text.x = element_text(size = 8),axis.text.y = element_text(size = 8), 
        plot.title = element_text(face="bold", size=10,hjust = 0.5,vjust = -1))  +
  scale_y_continuous(limits = c(0,60)) +
  ggtitle("Expectations about Outcome") 
exp

pdf("Figure3.pdf")
grid.arrange(voteref, exp, ncol=2, nrow=2)
dev.off()


##########
#Figure 4#
##########


rm(list = ls())

df <- read.dta("resultsFig4.dta")




resultsFig4<-ggplot(df, aes(x=estimate, y = id4)) +
  geom_point(size = 3) +
  geom_errorbarh(aes(xmax = max95, xmin = min95), height=.3) +
  scale_x_continuous(limits = c(-.6, .6))+
  geom_vline(xintercept = 0, 
             linetype = 2, color = "black")+ xlab("Change in Probability of voting No") + ylab("")


pdf("Figure4.pdf")
resultsFig4
dev.off()


##########
#Figure 5#
##########

#See Matching.R


##########
#Figure 6#
##########


rm(list = ls())


df3 <- read.dta("interpref.dta")


ggplot(df3,aes(x=stayeuro, y=estimate, fill=expectations)) +
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", # Use black outlines,
           size=.3) +
  geom_errorbar(aes(ymin=min95, ymax=max95),
                size=.3,    # Thinner lines
                width=.3,
                position=position_dodge(.9)) +
  scale_fill_grey(name="Expectations",labels=c("Grexit", "Negotiations"), start=0.2, end=0.7)+
  ggtitle("Effect of Expectations Conditional on Euro preferences") +
  ylab("Change in Prob. of Voting No Grext/Negoations Vs DK/DA") +
  scale_x_continuous(name="Stay in Euro?",breaks = c(1.5,5.5), labels = c("No", "Yes")) +
  theme(axis.title.x = element_text(size = 8), axis.title.y = element_text(size = 8), 
        axis.text.x = element_text(size = 8),axis.text.y = element_text(size = 8), 
        plot.title = element_text(face="bold", size=10,hjust = 0.5,vjust = -1))  
ggsave("Figure6.pdf", width = 22, height = 18, units = "cm")  

##########
#Figure 7#
##########


rm(list = ls())


polls <- read.dta("pollsR.dta")
polls$above = polls$time>=2.5

fig1full=ggplot(polls, aes(x = time, y = yes2,shape=above)) + 
  geom_point() +  ylim(10,60) + scale_shape_discrete(name  ="Bank Closure", labels=c("Before", "After"))
figure1full=fig1full + stat_smooth(method = "loess",span=1) +
  geom_vline(xintercept = 2.5, linetype = "dashed") +
  xlab("Date of poll fieldwork") + annotate("text", x = 3.25, y = 25, label = "Bank shutdown", size=5)   + 
  scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9), labels = c("27-Jun", "28-Jun", "29-Jun", "30-Jun", "1-Jul", "2-Jul", "3-Jul", "4-Jul", "5-Jul")) +
  ylab("% Yes vote")  + 
  theme(axis.title.x = element_text(size = 10), axis.title.y = element_text(size = 10), 
        axis.text.x = element_text(size = 10),axis.text.y = element_text(size = 10), 
        plot.title = element_text(face="bold", size=10,hjust = 0.5,vjust = -1),
        legend.justification = c(.01, .01), legend.position = c(.85, .05))  
figure1full
ggsave("Figure7.pdf", width = 22, height = 18, units = "cm")  


##########
#Figure 8#
##########


df2 <- read.dta("results8a.dta")
df3 <- read.dta("results8b.dta")



Fig8a<-ggplot(df2, aes(x=estimate, y = id4)) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmax = max95, xmin = min95), height=.3) +
  scale_x_continuous(limits = c(-.65, .65))+
  geom_vline(xintercept = 0, 
             linetype = 2, color = "black")+ xlab("Pr: Grexit Vs DK/DA") + ylab("") + 
  theme(axis.title.x = element_text(size = 10), axis.title.y = element_text(size = 10), 
        axis.text.x = element_text(size = 10),axis.text.y = element_text(size = 10), 
        plot.title = element_text(face="bold", size=10,hjust = 0.5,vjust = -1),
        legend.justification = c(.01, .01), legend.position = c(.85, .05))
Fig8a



Fig8b<-ggplot(df3, aes(x=estimate, y = id4)) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmax = max95, xmin = min95), height=.3) +
  scale_x_continuous(limits = c(-.65, .65))+
  geom_vline(xintercept = 0, 
             linetype = 2, color = "black")+ xlab("Pr: Negotiations Vs DK/DA") + ylab("") + 
  theme(axis.title.x = element_text(size = 10), axis.title.y = element_text(size = 10), 
        axis.text.x = element_text(size = 10),axis.text.y = element_text(size = 10), 
        plot.title = element_text(face="bold", size=10,hjust = 0.5,vjust = -1),
        legend.justification = c(.01, .01), legend.position = c(.85, .05))  
Fig8b


pdf("Figure8.pdf")
grid.arrange(Fig8a, Fig8b, ncol=2, nrow=1)
dev.off()


############
############
##Appendix##
############
############

###########
#Figure A8#
###########



rm(list = ls())

schaub <- read.dta("schaub.dta")

expsch<-ggplot(schaub, aes(x=estimate, y = id)) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmax = max95, xmin = min95), height=.3) +
  scale_x_continuous(limits = c(-1, 1))+
  geom_vline(xintercept = 0, 
             linetype = 2, color = "black")+ xlab("Treatment Effect") + ylab("") + 
  scale_y_continuous(breaks = c(1,2,3), labels = c("Varoufakis", "Draghi", "Schauble")) +
  theme(axis.title.x = element_text(size = 10), axis.title.y = element_text(size = 10), 
        axis.text.x = element_text(size = 10),axis.text.y = element_text(size = 10), 
        plot.title = element_text(face="bold", size=10,hjust = 0.5,vjust = -1),
        legend.justification = c(.01, .01), legend.position = c(.85, .05))  +
  ggtitle("Effect on willingness \n to pay the debt (1-5 scale)")
expsch
ggsave("FigureA8.pdf", width = 22, height = 18, units = "cm")  


##########
#Table A7#
##########


mediation <- read.dta("mediat.dta")
set.seed(2017)

beta1 <- lm(grexitbelief ~ bankholidaychange , data=mediation)
gamma <- lm(referendumno ~ bankholidaychange + grexitbelief, data=mediation)


med<-mediate(beta1, gamma, sims = 1000,
             boot = FALSE, boot.ci.type = "perc",
             treat = "bankholidaychange", mediator = "grexitbelief")


summary(med)
plot(med)


